package com.example.demo.controller;


import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/param9")
    public String param9(String name, @RequestPart("myfile")MultipartFile file) throws IOException {
        // @RequestPart 中设置的是传递过去的文件的名称
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        // 生成 文件保存地址
        System.out.println(UUID.randomUUID() + fileName);
        System.out.println(ClassUtils.getDefaultClassLoader().getResource("").getPath());
//        System.out.println(ClassUtils.getDefaultClassLoader().getResource("static").getPath());

//        String filePath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+
//                "/" + UUID.randomUUID() + fileName;
        String filePath = "./"+fileName;
        // 这几个函数方法有待了解
        // 保存文件 操作
        file.transferTo(new File(filePath));
        return filePath + "上传成功。";

    }





    @PostMapping("/m6/{name}/{password}")
    public Object method_6(@PathVariable String name,@PathVariable String password){
        System.out.println("name: "+name);
        System.out.println("password: "+password);
        return "redirect:/index.html";
    }


    @RequestMapping(value = "/m5", method = RequestMethod.POST)
    public Object method_5(Person person) {
        System.out.println("Person：" + person);
        System.out.println(person.getName());
        System.out.println(person.getId());
        System.out.println(person.getPassword());

        return "redirect:/index.html";
    }



    @RequestMapping("/m4")
    public Object method_4(@RequestParam(value = "time", required = false) String createtime) {
        System.out.println("时间：" + createtime);
        return "/index.html";
    }


    @RequestMapping("/sayhi")
    public String sayHi(){
        return "<h1>Hi,Spring MVC.<h1>";
        //return "Hi,Spring Boot.";
    }


    @RequestMapping("/m1")
    public Object method_1(String name, Integer num, Integer val){
        System.out.println("参数 name："+name);
        System.out.println("参数 num："+num);
        System.out.println("参数 val："+val);
        return "/index.html";
    }
}
